package com.sangshen.ad_jg_flutter_sdk;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import cn.jiguang.jgssp.ad.ADJgRewardVodAd;
import cn.jiguang.jgssp.ad.data.ADJgRewardVodAdInfo;
import cn.jiguang.jgssp.ad.error.ADJgError;
import cn.jiguang.jgssp.ad.listener.ADJgRewardVodAdListener;
import cn.jiguang.jgssp.util.ADJgAdUtil;
import java.util.Map;
import cn.jiguang.jgssp.ad.entity.ADJgExtraParams;
import cn.jiguang.jgssp.ad.entity.ADJgRewardExtra;
import java.util.HashMap;

public class FlutterRewardVodAd extends FlutterAd.FlutterOverlayAd implements FlutterDestroyableAd {

    @NonNull
    private final AdInstanceManager manager;
    /**
     * 广告位id
     */
    @NonNull
    private final String adPosId;

    private final Map<String, Object> rewardValidationData;

    /**
     * 场景id可选
     */
    @NonNull
    private final String sceneId;
    /**
     * 广告对象
     */
    private ADJgRewardVodAd adJgRewardVodAd;
    /**
     * 激励视频对象
     */
    private ADJgRewardVodAdInfo _rewardVodAdInfo;

    @Override
    void show() {
        if (_rewardVodAdInfo != null) {
            // TODO 激励视频广告对象一次成功拉取的广告数据只允许展示一次
            ADJgAdUtil.showRewardVodAdConvenient(manager.activity, _rewardVodAdInfo);
        }
    }

    static class Builder {
        @Nullable
        private AdInstanceManager manager;
        @Nullable
        private String adPosId;
        @Nullable
        private String sceneId;
        private Map<String, Object> rewardValidationData;

        public Builder setManager(@NonNull AdInstanceManager manager) {
            this.manager = manager;
            return this;
        }

        public Builder setAdPosId(@NonNull String adPosId) {
            this.adPosId = adPosId;
            return this;
        }

        public Builder setRewardValidationData(Map<String, Object> rewardValidationData) {
            this.rewardValidationData = rewardValidationData;

//            // 解析数据
//            String userId = (String) rewardValidationData.get("userId");
//            String custom = (String) rewardValidationData.get("custom");
//            String rewardName = (String) rewardValidationData.get("rewardName");
//            int rewardAmount = (int) rewardValidationData.get("rewardAmount");
//
//            // 处理解析后的数据
//            System.out.println("Received userId: " + userId);
//            System.out.println("Received custom: " + custom);
//            System.out.println("Received rewardName: " + rewardName);
//            System.out.println("Received rewardAmount: " + rewardAmount);

            return this;
        }

        public Builder setSceneId(@NonNull String sceneId) {
            this.sceneId = sceneId;
            return this;
        }

        FlutterRewardVodAd build() {
            if (manager == null) {
                throw new IllegalStateException("AdInstanceManager cannot not be null.");
            } else if (adPosId == null) {
                throw new IllegalStateException("adPosId cannot not be null.");
            }

            final FlutterRewardVodAd rewardVodAd = new FlutterRewardVodAd(
                    manager,
                    adPosId,
                    sceneId,
                    rewardValidationData
            );
            return rewardVodAd;
        }
    }

    private FlutterRewardVodAd(
            @NonNull AdInstanceManager manager,
            @NonNull String adPosId,
            @NonNull String sceneId,
            Map<String, Object> rewardValidationData
    ) {
        this.manager = manager;
        this.adPosId = adPosId;
        this.sceneId = sceneId;
        this.rewardValidationData = rewardValidationData;
    }

    @Override
    void load() {
        adJgRewardVodAd = new ADJgRewardVodAd(manager.activity);
        // 设置仅支持的广告平台，设置了这个值，获取广告时只会去获取该平台的广告，null或空字符串为不限制，默认为null，方便调试使用，上线时建议不设置
        adJgRewardVodAd.setOnlySupportPlatform(ADJgDemoConstant.REWARD_VOD_AD_ONLY_SUPPORT_PLATFORM);

        if (this.rewardValidationData != null) {
            try {
                // 解析数据
                String userId = (String) rewardValidationData.get("userId");
                String custom = (String) rewardValidationData.get("custom");
                String rewardName = (String) rewardValidationData.get("rewardName");
                int rewardAmount = (int) rewardValidationData.get("rewardAmount");

                ADJgRewardExtra extraParams = new ADJgRewardExtra(userId);
                // 设置激励视频服务端验证的自定义信息
                extraParams.setCustomData(custom);
                // 设置激励视频服务端激励名称
                extraParams.setRewardName(rewardName);
                // 设置激励视频服务端激励数量
                extraParams.setRewardAmount(rewardAmount);

                // 激励视频服务端回调的参数设置
                Map<String, String> rewardCallbackExtraData = new HashMap<>();
                // 开发者系统中的⽤户id，会在请求客户的回调url中带上，注意"thirdUserId为固定写法
                rewardCallbackExtraData.put("thirdUserId", userId);
                // 开发者⾃定义的附加参数，会在请求客户的回调url中带上，注意"extraData"为固定写法
                rewardCallbackExtraData.put("extraData", custom);

                extraParams.setRewardCallbackExtraData(rewardCallbackExtraData);

                adJgRewardVodAd.setLocalExtraParams(new ADJgExtraParams.Builder().rewardExtra(extraParams).build());

                // 处理解析后的数据
                System.out.println("Received userId: " + userId);
                System.out.println("Received custom: " + custom);
                System.out.println("Received rewardName: " + rewardName);
                System.out.println("Received rewardAmount: " + rewardAmount);

            } catch (Exception e) {

            }
        }

        // 设置激励视频广告监听
        adJgRewardVodAd.setListener(new ADJgRewardVodAdListener() {
            @Override
            public void onVideoCache(ADJgRewardVodAdInfo rewardVodAdInfo) {
                // 目前汇量和Inmobi走了该回调之后才准备好
                Log.d(ADJgDemoConstant.TAG, "onVideoCache...");
            }

            @Override
            public void onVideoComplete(ADJgRewardVodAdInfo rewardVodAdInfo) {
                Log.d(ADJgDemoConstant.TAG, "onVideoComplete...");
            }

            @Override
            public void onVideoError(ADJgRewardVodAdInfo rewardVodAdInfo, ADJgError adJgError) {
                Log.d(ADJgDemoConstant.TAG, "onVideoError..." + adJgError.toString());
            }

            @Override
            public void onReward(ADJgRewardVodAdInfo rewardVodAdInfo) {
                Log.d(ADJgDemoConstant.TAG, "onReward...");
                manager.onAdReward(
                        FlutterRewardVodAd.this,
                        rewardVodAdInfo.getPlatform(),
                        rewardVodAdInfo.getEcpmPrecision(),
                        rewardVodAdInfo.getECPM(),
                        rewardVodAdInfo.getRewardMap()
                );
            }

            @Override
            public void onAdReceive(ADJgRewardVodAdInfo rewardVodAdInfo) {
                _rewardVodAdInfo = rewardVodAdInfo;
                Log.d(ADJgDemoConstant.TAG, "onAdReceive...");
                manager.onAdReceive(
                        FlutterRewardVodAd.this,
                        rewardVodAdInfo.getPlatform(),
                        rewardVodAdInfo.getEcpmPrecision(),
                        rewardVodAdInfo.getECPM()
                );
            }

            @Override
            public void onAdExpose(ADJgRewardVodAdInfo rewardVodAdInfo) {
                Log.d(ADJgDemoConstant.TAG, "onAdExpose...");
                manager.onAdExpose(
                        FlutterRewardVodAd.this,
                        rewardVodAdInfo.getPlatform(),
                        rewardVodAdInfo.getEcpmPrecision(),
                        rewardVodAdInfo.getECPM()
                );
            }

            @Override
            public void onAdClick(ADJgRewardVodAdInfo rewardVodAdInfo) {
                Log.d(ADJgDemoConstant.TAG, "onAdClick...");
                manager.onAdClick(
                        FlutterRewardVodAd.this,
                        rewardVodAdInfo.getPlatform(),
                        rewardVodAdInfo.getEcpmPrecision(),
                        rewardVodAdInfo.getECPM()
                );
            }

            @Override
            public void onAdClose(ADJgRewardVodAdInfo rewardVodAdInfo) {
                Log.d(ADJgDemoConstant.TAG, "onAdClose...");
                manager.onAdClose(
                        FlutterRewardVodAd.this,
                        rewardVodAdInfo.getPlatform(),
                        rewardVodAdInfo.getEcpmPrecision(),
                        rewardVodAdInfo.getECPM()
                );
            }

            @Override
            public void onAdFailed(ADJgError adJgError) {
                if (adJgError != null) {
                    String failedJosn = adJgError.toString();
                    Log.d(ADJgDemoConstant.TAG, "onAdFailed..." + failedJosn);
                    manager.onAdFailed(FlutterRewardVodAd.this, adJgError);
                }
            }
        });

        // 设置场景id
        adJgRewardVodAd.setSceneId(sceneId);
        // 加载广告，参数为广告位ID
        adJgRewardVodAd.loadAd(adPosId);
    }

    @Override
    public void release() {
        Log.d(ADJgDemoConstant.TAG, " rewardAd release ");
        if (adJgRewardVodAd != null) {
            adJgRewardVodAd.release();
            adJgRewardVodAd = null;
        }
    }
}
